<template>
    <div class="side-menu-wrapper">
        <slot></slot>
        <Menu :accordion="accordion" :active-name="activeName" :open-names="openedNames" :theme="theme" @on-select="handleSelect"
              ref="menu" v-if="!collapsed" width="auto">
            <template v-for="item in menuList">
                <template>
                    <RuiMenuItem :key="`menu-${item.name}`" :parent-item="item"
                                    v-if="showChildren(item)"></RuiMenuItem>
                    <menu-item :key="`menu-${item.name}`" :name="getNameOrHref(item)" v-else>
                        <common-icon :type="item.icon || ''"/>
                        <span>{{ showTitle(item) }}</span></menu-item>
                </template>
            </template>
        </Menu>
        <div :list="menuList" class="menu-collapsed" v-if="collapsed">
            <template v-for="item in menuList">
                <collapsed-menu :icon-size="iconSize" :key="`drop-menu-${item.name}`" :parent-item="item"
                                :root-icon-size="rootIconSize" :theme="theme" @on-click="handleSelect" hide-title
                                v-if="item.children && item.children.length > 1"></collapsed-menu>
                <Tooltip :content="showTitle(item.children && item.children[0] ? item.children[0] : item)" :key="`drop-menu-${item.name}`"
                         placement="right"
                         transfer v-else>
                    <a :style="{textAlign: 'left'}" @click="handleSelect(getNameOrHref(item, true))"
                       class="drop-menu-a">
                        <common-icon :color="textColor" :size="rootIconSize"
                                     :type="item.icon || (item.children && item.children[0].icon)"/>
                    </a>
                </Tooltip>
            </template>
        </div>
    </div>
</template>
<script>
    import RuiMenuItem from '../rui-menu-item.vue'
    import CollapsedMenu from './collapsed-menu.vue'
    import {getUnion} from '@/libs/tools'
    import mixin from '../minxin/mixin'

    export default {
        name: 'SideMenu',
        mixins: [mixin],
        components: {
            RuiMenuItem,
            CollapsedMenu
        },
        props: {
            menuList: {
                type: Array,
                default() {
                    return []
                }
            },
            collapsed: {
                type: Boolean
            },
            theme: {
                type: String,
                default: 'light'
            },
            rootIconSize: {
                type: Number,
                default: 20
            },
            iconSize: {
                type: Number,
                default: 16
            },
            accordion: Boolean,
            activeName: {
                type: String,
                default: ''
            },
            openNames: {
                type: Array,
                default: () => []
            }
        },
        data() {
            return {
                openedNames: []
            }
        },
        methods: {
            handleSelect(name) {
                this.$emit('on-select', name)
            },
            getOpenedNamesByActiveName(name) {
                let names = this.$store.getters.breadCrumbList.map(item=>item.name)
                names.splice(0,1);
                return names
            },
            updateOpenName(name) {
                if (name === this.$config.getHomeName()) {
                    this.openedNames = []
                } else {
                    this.openedNames = this.getOpenedNamesByActiveName(name)
                }
            }
        },
        computed: {
            textColor() {
                return this.theme === 'dark' ? '#fff' : '#495060'
            }
        },
        watch: {
            activeName(name) {
                if (this.accordion) this.openedNames = this.getOpenedNamesByActiveName(name)
                else this.openedNames = getUnion(this.openedNames, this.getOpenedNamesByActiveName(name))
            },
            openNames(newNames) {
                this.openedNames = newNames
            },
            openedNames() {
                this.$nextTick(() => {
                    this.$refs.menu.updateOpened()
                })
            }
        },
        mounted() {
            this.openedNames = getUnion(this.openedNames, this.getOpenedNamesByActiveName(name))
        }
    }
</script>
<style lang="less" scoped>
    @import 'side-menu.less';
</style>
